package algorithm.graph;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;

public class SparseGraph implements Graph {


    //节点个数
    private int n;
    //边的个数
    private int m;
    //是否为有向图
    private boolean directed;

    private List<Integer>[] g;

    public SparseGraph(int count,boolean directed){
        this.n = count;
        this.directed = directed;
        g = new LinkedList[count];
        for (int i = 0; i < count; i++) {
            g[i] = new LinkedList<>();
        }
    }

    @Override
    public int V() {
        return n;
    }

    @Override
    public int E() {
        return m;
    }

    @Override
    public void addEdge(int v, int w) {

        g[v].add(w);
        if (!directed){
            g[w].add(v);
        }
        m++;
    }

    @Override
    public boolean hasEdge(int v, int w) {
        List<Integer> list = g[v];
        for (Integer anInt : list) {
            if (anInt-w==0){
                return true;
            }
        }
        return false;
    }


    @Override
    public Iterator<Integer> iterator(int v){
        return g[v].iterator();
    }

    @Override
    public void show() {
        for (List<Integer> list : g) {
            for (Integer integer : list) {
                System.out.print(integer+"\t");
            }
            System.out.println("");
        }
    }

}
